//
//  LLMainController.swift
//  WeiBoShiBing
//
//  Created by Bingo on 15/10/27.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import UIKit

class LLMainController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.tintColor = UIColor.orangeColor()
        //首页
        let homeVc = LLHomeViewController()
        addChildViewController(homeVc, title: "首页", imageName: "tabbar_home")
        //消息
        let messageVc = LLMessageViewController()
        self.addChildViewController(messageVc, title: "消息", imageName: "tabbar_message_center")
        /// 加号
        let controller  = UIViewController()
        self.addChildViewController(controller, title: "", imageName: "f")
        
        /// 发现
        let discover = LLDiscoverViewController()
        self.addChildViewController(discover, title: "发现", imageName:
            "tabbar_discover")
        /// 我
        let profile = LLProfileViewController()
        addChildViewController(profile , title:"我",imageName:"tabbar_profile")
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //发表说说按钮frame
        let width = tabBar.bounds.width / CGFloat(5)
        composeButton.frame = CGRect(x: width * 2, y: 0, width: width, height: tabBar.bounds.height)
        
        tabBar.addSubview(composeButton)
        
    }
    /**
    - parameter controller:对应的控制器
    - parameter title:      顶部标题
    - parameter imageName:  图片名称
    */
    private func addChildViewController(controller: UIViewController ,title : String ,imageName:String){
        controller.title = title
        
        controller.tabBarItem.image = UIImage(named:imageName)
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    lazy var composeButton :UIButton = {
        let button = UIButton()
        //发表说说按钮图片
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //发表说说背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    //绑定发表说说点击事件
    func composeButtonClick(){
        print(__FUNCTION__)
        
        let composeController = LLComposeViewController()
        
        presentViewController(UINavigationController(rootViewController: composeController), animated: true, completion: nil)
    }
    
}
